package com.lan.app.api;


import com.lan.app.repository.User;
import com.lan.app.service.IUserService;
import com.lan.app.constant.Constant;
import com.lan.support.intercept.an.Auth;
import com.lan.support.response.json.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("api")
public class LoginApi {

    @Resource
    private IUserService userService;


    @ResponseBody
    @RequestMapping("login")
    public R login(HttpSession session, @RequestParam String userName, @RequestParam String pwd) {
        User param = new User();
        param.setUserName(userName);
        param.setPwd(pwd);
        User user = userService.login(param);
        user.setPwd(null);
        session.setAttribute(Constant.User_Info, user);
        return R.OK(user);
    }


    @Auth
    @ResponseBody
    @RequestMapping("logout")
    public R logout(HttpSession session) {
        session.invalidate();
        return R.OK();
    }
}
